
#include <assert.h>
#include <stdio.h>

#include "processor.h"
#include "epollmanager.h"
#include "epolleventhandler.h"
#include "buffersocket.h"

EpollEventHandler :: EpollEventHandler( EpollManager * ptEpollEventMng, Processor * ptProcessor )
{
	assert( ptEpollEventMng != NULL );
	m_ptEpollEventMng = ptEpollEventMng;
	m_ptProcessor = ptProcessor;
}

EpollEventHandler :: ~EpollEventHandler()
{
}

int EpollEventHandler :: Handle( epoll_event * ptEvent )
{
	int ret = -1;
	BufferSocket * pBufSocket = (BufferSocket *) ptEvent->data.ptr;

	if ( pBufSocket->GetFd() == m_ptEpollEventMng->GetListenFd() )
	{
		//printf("accept\n");
		ret = OnAccept( ptEvent );
	}
	else
	{
		switch( ptEvent->events )
		{
			case EPOLLOUT:
				ret = OnWrite( ptEvent );
				break;
			case EPOLLIN:	
				ret = OnRead( ptEvent );
				break;
		}
	}	
	return ret;
}
